<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Adverti extends Controller
{
	public  function index(){
		
		$list = Db::name('Adverti')->select();
		return view('Adverti/index',[
			'list' => $list,
			]);
	}
	public  function add(){

	
        return view('Adverti/ljadd');
    }
	
	public function cadd(Request $request){
		$p = $request->post();
		// var_dump(request()->file('image'));die;
		if (!is_dir('static/uploads'.DS.date('Ymd',time()))) {
			mkdir('static/uploads/'.date('Ymd',time()));
		} 
	    
	    $file = \think\Image::open(request()->file('image'));
	   
	    $img = 'static/uploads/'.date('Ymd',time()).'/'.rand().'.jpg';
	    $file->thumb(150, 150,6)->save($img);
		// var_dump($file);die;
		$data = ['aname' => $p['ljname'],'http' => $p['ljaddress'] ,'aimage' => $img];
		$list = Db::name('Adverti')->insert($data);
		if ($list) {
			return $this->success('添加成功',url('index/Adverti/index'));
		}
	}

	public function delect($id){
		// var_dump($id);die;
		$list = Db::table('Adverti')->delete($id);
		if ($list) {
			return $this->success('删除成功');
		} else {
			return $this->error('删除失败');
		}

	}
	public function shang($id)
	{	
		
		$list = Db::name('adverti')->where('id',$id)->update(['status'=>'1']);
		if ($list > 0) {
			return $this->success('更改成功',url('index/adverti/index'));
		} else {
			return $this->seeor('更改失败');
		}
	}
	public function xia($id)
	{
		$ll = Db::name('adverti')->field('id')->select();
		foreach ($ll as $key => $value) {
			$ll = $value['id'];
			$mu = Db::name('adverti')->where('id',$ll)->update(['status'=>'1']);

		}
		$list = Db::name('adverti')->where('id',$id)->update(['status'=>'0']);
		if ($list > 0) {
			return $this->success('更改成功',url('index/adverti/index'));
		} else {
			return $this->error('更改失败');
		}
	}
}